package com.yanqu.road.server.cmd.grandchild.exam;

import com.yanqu.road.pb.grandchild.GrandChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.grandson.GrandChildModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_GRAND_CHILD_JOIN_IMPERIAL_EXAM, desc = "参加科举考试")
public class JoinImperialExamCmd extends AbstractCommand {

    private String childId;
    private String examNo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GrandChildProto.JoinExamReqMsg reqMsg = GrandChildProto.JoinExamReqMsg.parseFrom(packet.getBytes());
        childId = reqMsg.getId();
        examNo = reqMsg.getNo();
        if(StringUtils.isNullOrEmpty(examNo)){
            return false;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(GrandChildModule.class).joinExam(childId, examNo);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GrandChildProto.JoinExamRespMsg.Builder builder = GrandChildProto.JoinExamRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GRAND_CHILD_JOIN_IMPERIAL_EXAM, builder);
        }
    }
}
